﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace ComUIControl
{
    public partial class DegreeTrackBar : UserControl
    {
        public DegreeTrackBar()
        {
            InitializeComponent();
            textBoxDegree.DataBindings.Add("Text", this, "Degree");
            trackBarDegree.Minimum = (int)MinValue;
            trackBarDegree.Maximum = (int)MaxValue;
        }

        private int _minValue = 0;
      
        [DefaultValue(0)]
        public int MinValue
        {
            get
            {
                return _minValue;
            }
            set
            {
                _minValue = value;
                if (trackBarDegree != null)
                {
                    trackBarDegree.Minimum = (int)value;
                }
            }
        }

        private int _maxValue = 100;
        [DefaultValue(100)]
        public int MaxValue
        {
            get
            {
                return _maxValue;
            }
            set
            {
                _maxValue = value;
                if (trackBarDegree != null)
                {
                    trackBarDegree.Maximum = value;
                }
            }
        }

        private int _nDegree = 1;
        public int Degree
        {
            get
            {
                return _nDegree;
            }
            set
            {
                if (value != _nDegree)
                {
                    _nDegree = value;
                    trackBarDegree.Value = value;
                    textBoxDegree.Text = value.ToString();
                    RaiseDegreeChanged("Degree");
                }
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;
        public delegate void UpdatePropertyDelegate(string strName);
        private void RaiseDegreeChanged(string propertyName)
        {
            if (this.InvokeRequired)
            {
                UpdatePropertyDelegate outdelegate = new UpdatePropertyDelegate(RaiseDegreeChanged);
                this.BeginInvoke(outdelegate, new object[] { propertyName });
                return;
            }
            var handler = this.PropertyChanged;
            if (handler != null)
            {
                handler(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        private void trackBar1_Scroll(object sender, EventArgs e)
        {
            Degree = trackBarDegree.Value;
        }

        private void textBoxDegree_Validating(object sender, CancelEventArgs e)
        {
            int val;
            if (int.TryParse(textBoxDegree.Text, out val))
            {
                if (val < trackBarDegree.Minimum || val > trackBarDegree.Maximum)
                {
                    string infoErr = string.Format("请输入一个[{0},{1}]范围内的整数.", MinValue, MaxValue);
                    errorProvider1.SetError(textBoxDegree, infoErr);
                }
                else
                {
                    //Value = val;
                    errorProvider1.SetError(textBoxDegree, "");
                }
            }
            else
            {
                errorProvider1.SetError(textBoxDegree, "不是合法的数字!");
                textBoxDegree.Focus();
            }
        }

        private void textBoxDegree_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == '\b'
               || Char.IsDigit(e.KeyChar)
               || Char.IsControl(e.KeyChar)
               || e.KeyChar == 46
               || e.KeyChar == 45 )
            {
                e.Handled = false;
            }
            else
            {
                e.Handled = true;
            }
        }
    }
}
